const chatService = require('../services/chatService');
const ResultWrapper = require('../utils/resultWrapper');

class ChatController {
  static async visionChat(req, res) {
    try {
      const imageFile = req.files.image[0];
      const question = req.body.question;

      // 将图片文件转换为base64格式
      const imageBase64 = imageFile.buffer.toString('base64');
      
      const response = await chatService.visionChat(question, imageBase64, imageFile.mimetype);
      res.json(response);
    } catch (error) {
      console.error('视觉聊天处理失败:', error);
      res.status(500).json(ResultWrapper.error(error.message));
    }
  }

  static async chat(req, res) {
    try {
      const { humanMessage, systemMessage } = req.body;
      const response = await chatService.chat(humanMessage, systemMessage);
      res.json(ResultWrapper.success(response));
    } catch (err) {
      res.status(500).json(ResultWrapper.error(err.message));
    }
  }
  static async ragChat(req, res) {
    try {
      const { message } = req.body;
      
      if (!message) {
        return res.status(400).json({ error: '消息内容不能为空' });
      }

      const response = await chatService.ragChat(message);
      res.json(response);
    } catch (error) {
      res.status(500).json({ error: '服务器内部错误' });
    }
  }
}

module.exports = ChatController;